package com.sbj.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sbj.dto.req.GoodsReq;
import com.sbj.dto.res.CategoryRes;
import com.sbj.dto.res.GoodsRes;
import com.sbj.entity.Goods;
import com.sbj.service.CategoryService;
import com.sbj.service.GoodsService;
import com.zmh.framework.config.BaseCommonInterface;
import com.zmh.framework.util.Result;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/9 13:51
 */
@RestController
@RequestMapping(value = "/goods")
@AllArgsConstructor
@Slf4j
public class GoodsController implements BaseCommonInterface<GoodsReq, GoodsRes> {

    private final GoodsService goodsService;

    @Override
    public Result<String> save(@Valid GoodsReq goodsReq) {
        goodsService.saveOrUpdate(Convert.convert(Goods.class,goodsReq));
        return Result.successResult();
    }

    @Override
    public Result<String> del(Integer id) {
        goodsService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<GoodsRes>> lists(GoodsReq goodsReq) {

        return Result.successResult(goodsService.lists(goodsReq));
    }

    @Override
    public Result<GoodsRes> findById(Integer id) {
        return Result.successResult(Convert.convert(GoodsRes.class,goodsService.getById(id)));
    }

    @Override
    public Result<String> batchDel(List<GoodsRes> list) {
        return null;
    }

    private final CategoryService categoryService;

    @GetMapping(value = "/categoryList")
    public Result<List<CategoryRes>>  categoryList(){
        return Result.successResult(Convert.toList(CategoryRes.class,categoryService.list()));
    }

    /**
     * 更新上架状态
     * @param goodsReq
     * @return
     */
    @PostMapping(value = "/changeListed")
    public Result<String> changeListed(@RequestBody GoodsReq goodsReq){
        goodsService.changeListed(goodsReq);
        return Result.successResult();
    }

    /**
     * 批量上架、下架
     * @param goodsReq
     * @return
     */
    @PostMapping(value = "/batchListed")
    public Result<String> batchListed(@RequestBody List<GoodsReq> goodsReqList){
        goodsService.batchListed(goodsReqList);
        return Result.successResult();
    }
}
